package org.jivesoftware.smackx.packet;

import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.util.StringUtils;
import org.xmlpull.v1.XmlPullParser;

public class LastActivity extends IQ
{
  public long lastActivity = -1L;
  public String message;

  public LastActivity()
  {
    setType(IQ.Type.GET);
  }

  public static LastActivity getLastActivity(Connection paramConnection, String paramString)
    throws XMPPException
  {
    LastActivity localLastActivity1 = new LastActivity();
    localLastActivity1.setTo(StringUtils.parseBareAddress(paramString));
    PacketCollector localPacketCollector = paramConnection.createPacketCollector(new PacketIDFilter(localLastActivity1.getPacketID()));
    paramConnection.sendPacket(localLastActivity1);
    LastActivity localLastActivity2 = (LastActivity)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localLastActivity2 == null)
      throw new XMPPException("No response from server on status set.");
    if (localLastActivity2.getError() != null)
      throw new XMPPException(localLastActivity2.getError());
    return localLastActivity2;
  }

  private void setMessage(String paramString)
  {
    this.message = paramString;
  }

  public String getChildElementXML()
  {
    StringBuilder localStringBuilder = new StringBuilder();
    localStringBuilder.append("<query xmlns=\"jabber:iq:last\"");
    if (this.lastActivity != -1L)
      localStringBuilder.append(" seconds=\"").append(this.lastActivity).append("\"");
    localStringBuilder.append("></query>");
    return localStringBuilder.toString();
  }

  public long getIdleTime()
  {
    return this.lastActivity;
  }

  public String getStatusMessage()
  {
    return this.message;
  }

  public void setLastActivity(long paramLong)
  {
    this.lastActivity = paramLong;
  }

  public static class Provider
  implements IQProvider
  {
    public IQ parseIQ(XmlPullParser paramXmlPullParser)
      throws Exception
    {
      if (paramXmlPullParser.getEventType() != 2)
        throw new IllegalStateException("Parser not in proper position, or bad XML.");
      LastActivity localLastActivity = new LastActivity();
      try
      {
        String str1 = paramXmlPullParser.getAttributeValue("", "seconds");
        String str2 = paramXmlPullParser.nextText();
        if (str1 != null)
          localLastActivity.setLastActivity((int)new Double(str1).longValue());
        if (str2 != null)
          LastActivity.access$0(localLastActivity, str2);
        return localLastActivity;
      }
      catch (Exception localException)
      {
        localException.printStackTrace();
      }
    }
  }
}